﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace Enterprises.Wpf.Sample.ViewModel
{
    /// <summary>
    /// ObservableObject 实列
    /// 1、继承ObservableObject
    /// </summary>
    public partial class ObservableObjectDemoCtlViewModel : ObservableObject
    {
        private int counter;

        /// <summary>
        /// 1、简单使用，利用SetProperty
        /// </summary>
        public int Counter
        {
            get => counter;
            set => SetProperty(ref counter, value);  // 提供 SetProperty<T>(ref T, T, string) 的方法检查属性的当前值，并更新它（如果不同）
        }

        /// <summary>
        /// 2、利用ObservableProperty，简化代码，需要将类设置为partial
        /// 支持参数小写或者带_开通的属性,自动转为大驼峰. 这里后台自动会生产变量Increment
        /// </summary>
        [ObservableProperty]
        private int increment;

        /// <summary>
        /// 3、通知依赖属性
        /// </summary>
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(UserScore))]
        private string? userName = "张三";

        /// <summary>
        /// 用户分值
        /// </summary>
        public string UserScore => $"你好，{UserName}-{Counter}";


        /// <summary>
        /// 4、通知依赖命令. 控制一个命令是否可以执行.
        /// 这里需要定义一个Reset方法，不要加Command，加上特性 [RelayCommand]
        /// 使用[NotifyCanExecuteChangedFor(nameof(ResetCommand))]
        /// </summary>
        [ObservableProperty]
        [NotifyCanExecuteChangedFor(nameof(ResetCommand))]
        private bool resetIncrement;

        [RelayCommand(CanExecute = nameof(ResetIncrement))]
        private void Reset()
        {
            Increment = 1;
            Counter = 0;
        }

        /// <summary>
        /// 计算按钮
        /// </summary>
        public RelayCommand CounterAddCmd => new(() => Counter = Counter + Increment);
    }
}
